<?php
	class Assign_model extends CI_Model{
		
		public function __construct(){
			$this->load->database();
		}
		
		public function getAssignList($id=FALSE){
			$this->db->select('a.*, g.name');
			$this->db->from('user_assign a');
			$this->db->join('group g', 'a.gid = g.id', 'left');			
			$query = $this->db->get();
			if($id!=FALSE){				
				$this->db->where('a.id = '.$id, NULL, FALSE);
				return $query->row_array();
			}
			else return $query->result_array();	
		}
		
		public function getAssign($gid){
			$query = $this->db->get('user_assign');
			$query = $this->db->get_where('user_assign', array('gid' => $gid));
			return $query->row_array();
		}
		
		public function save(){
			$id = $this->input->post('id');
			$module_id = implode(",", $this->input->post('module_id'));			
			$data = array(
				"gid" => $this->input->post('gid'),
				"module_id" => $module_id
			);
			if(empty($id))
				return $this->db->insert('user_assign', $data);
			else{
				// Delete Old Record
				$this->db->where('id', $id);
				$this->db->delete('user_assign');
				
				// Insert new Record
				return $this->db->insert('user_assign', $data);
			}
		}
		
		public function delete($id){
			$this->db->where('id', $id);
			$this->db->delete('user_assign');
		}
		
	}
?>